package com.woniuxy.goods.controller;


import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.woniuxy.core.annotation.Permission;
import com.woniuxy.core.exception.ParamException;
import com.woniuxy.core.util.ObjectCoverUtil;
import com.woniuxy.core.util.Result;
import com.woniuxy.goods.dto.GoodsDto;
import com.woniuxy.goods.entity.Goods;
import com.woniuxy.goods.entity.GoodsCatPicture;
import com.woniuxy.goods.form.AddGoodsForm;
import com.woniuxy.goods.form.AddGoodsPictureForm;
import com.woniuxy.goods.form.FindGoodsPageForm;
import com.woniuxy.goods.form.UpdateGoodsForm;
import com.woniuxy.goods.param.AddGoodsParam;
import com.woniuxy.goods.param.AddGoodsPictureParam;
import com.woniuxy.goods.param.FindGoodsPageParam;
import com.woniuxy.goods.param.UpdateGoodsParam;
import com.woniuxy.goods.service.GoodsService;
import com.woniuxy.goods.util.TokenUtil;
import com.woniuxy.web.util.ConvertUtil;
import io.swagger.annotations.*;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * <p>
 *  前端控制器
 * </p>
 * 典当物品管理控制层接口
 * @author ...
 * @since 2024年05月22日
 */
@RestController
@Api(tags = "典当物品管理接口")
@RequestMapping("/goods")
public class GoodsController {
    /**
     * 典当物品管理服务层接口
     */
    @Resource
    private GoodsService goodsService;
    /**
     * 解析token工具类
     */
    @Resource
    private TokenUtil tokenUtil;

    /**
     * 新增典当物品
     * @param form 新增典当物品对象
     * @param token 携带当前登录管理员id的请求头
     * @return 典当物品对象
     * @throws Exception
     */
    @ApiOperation("新增典当物品")
    @ApiResponses({
            @ApiResponse(code = 200,message = "OK",response = GoodsDto.class)
    })
    @PostMapping("/addGoods")
    @Permission(roles = {"店长","店员"})
    public Result addGoods(@Valid @RequestBody AddGoodsForm form, BindingResult br,
                           @RequestHeader String token)throws Exception{
        if(br.hasErrors())throw  new ParamException(10001,"参数有误");
        Integer accountId = tokenUtil.getAccountId(token);
        Map map = JSON.parseObject(form.getAttribute(), Map.class);
        AddGoodsParam param = BeanUtil.toBean(form, AddGoodsParam.class);
        param.setAccountId(accountId);
        param.setGoodsAttribute(map);
        GoodsDto goods = goodsService.addGoods(param);
        return Result.success("新增典当物品成功",goods);
    }

    /**
     * 上传鉴定图
     * @param id 新增物品鉴定图id
     * @param picture 定义图文件
     * @param token 携带当前登录管理员id的请求头
     * @return 鉴定图地址
     * @throws Exception
     */
    @ApiOperation("上传鉴定图")
    @ApiResponses({
            @ApiResponse(code = 200,message = "OK",response = GoodsDto.class)
    })
    @Permission(roles = {"店长","店员"})
    @PostMapping(value = "/addGoodsPicture",consumes = "multipart/form-data")
    public Result addGoodsPicture(@Valid Integer id,
                           @ApiParam("定义图文件") @RequestPart("picture") MultipartFile picture,
                           @RequestHeader String token)throws Exception{
        Integer accountId = tokenUtil.getAccountId(token);
        AddGoodsPictureParam param = new AddGoodsPictureParam();
        param.setAccountId(accountId);
        param.setId(id);
        String image = goodsService.addGoodsPicture(param,picture);
        return Result.success("上传鉴定图成功",image);
    }

    /**
     * 修改鉴定图
     * @param id 修改物品鉴定图id
     * @param goodsId 物品id
     * @param picture 定义图文件
     * @param token 携带当前登录管理员id的请求头
     * @return 鉴定图地址
     * @throws Exception
     */
    @ApiOperation("修改鉴定图")
    @ApiResponses({
            @ApiResponse(code = 200,message = "OK",response = GoodsDto.class)
    })
    @Permission(roles = {"店长","店员"})
    @PostMapping(value = "/modifyGoodsPicture",consumes = "multipart/form-data")
    public Result modifyGoodsPicture(@Valid Integer id,@Valid Integer goodsId,
                                  @ApiParam("定义图文件") @RequestPart("picture") MultipartFile picture,
                                  @RequestHeader String token)throws Exception{
        Integer accountId = tokenUtil.getAccountId(token);
        System.out.println(id+"+"+goodsId);
        String image = goodsService.modifyGoodsPicture(accountId,goodsId,id,picture);
        return Result.success("修改鉴定图成功",image);
    }

    /**
     * 保存物品
     * @param token 携带当前登录管理员id的请求头
     * @return 物品dto对象
     * @throws Exception
     */
    @ApiOperation("保存物品")
    @ApiResponses({
            @ApiResponse(code = 200,message = "OK",response = GoodsDto.class)
    })
    @PostMapping(value = "/saveGoods")
    @Permission(roles = {"店长","店员"})
    public Result saveGoods(@RequestHeader String token)throws Exception{
        Integer accountId = tokenUtil.getAccountId(token);
        GoodsDto goods = goodsService.saveGoods(accountId);
        return Result.success("保存物品成功",goods);
    }

    /**
     * 修改典当物品状态
     * @param goodsId 典当物品对象id
     * @param stateNUm 状态码
     * @return 典当物品对象
     * @throws Exception
     */
    @ApiOperation("修改典当物品状态")
    @ApiImplicitParams({
            @ApiImplicitParam("典当物品对象id"),
            @ApiImplicitParam("状态码")
    })
    @ApiResponses({
            @ApiResponse(code = 200,message = "OK",response = GoodsDto.class)
    })
    @GetMapping("/updateGoodsStateNum")
    public Result updateGoodsStateNum(Integer goodsId,Integer stateNUm) throws Exception{
        GoodsDto goods = goodsService.updateGoodsStateNum(goodsId,stateNUm);
        return Result.success("修改典当物品状态成功",goods);
    }

    /**
     * 修改典当物品
     * @param goods 典当物品对象
     * @return 典当物品对象
     * @throws Exception
     */
    @ApiOperation("修改典当物品")
    @ApiImplicitParam("典当物品对象")
    @ApiResponses({
            @ApiResponse(code = 200,message = "OK",response = GoodsDto.class)
    })
    @Permission(roles = {"店长","店员"})
    @PostMapping("/updateGoodsByEntity")
    public Result updateGoodsByEntity(@RequestBody Goods goods) throws Exception{
        GoodsDto goodsDto = goodsService.updateGoodsByEntity(goods);
        return Result.success("修改典当物品成功",goodsDto);
    }

    /**
     * 修改典当物品流程
     * @param goodsId 典当物品对象id
     * @param stateNUm 状态码
     * @param token 携带当前登录管理员id的请求头
     * @return 典当物品对象
     * @throws Exception
     */
    @ApiOperation("修改典当物品流程")
    @ApiImplicitParams({
            @ApiImplicitParam("典当物品对象id"),
            @ApiImplicitParam("状态码")
    })
    @ApiResponses({
            @ApiResponse(code = 200,message = "OK",response = GoodsDto.class)
    })
    @GetMapping("/updateGoodsProcess")
    @Permission(roles = {"店长","店员"})
    public Result updateGoodsProcess(Integer goodsId,Integer stateNUm,@RequestHeader String token) throws Exception{
        Integer accountId = tokenUtil.getAccountId(token);
        System.out.println(goodsId+":"+stateNUm);
        GoodsDto goods = goodsService.updateGoodsProcess(goodsId,stateNUm,accountId);
        return Result.success("修改典当物品流程",goods);
    }

    /**
     * 修改典当物品
     * @param form 修改典当物品对象
     * @param token 携带当前登录管理员id的请求头
     * @return 典当物品对象
     * @throws Exception
     */
    @ApiOperation("修改典当物品")
    @ApiResponses({
            @ApiResponse(code = 200,message = "OK",response = GoodsDto.class)
    })
    @PostMapping("/updateGoods")
    public Result updateGoods(@Validated @RequestBody UpdateGoodsForm form,BindingResult br, @RequestHeader String token) throws Exception{
        if(br.hasErrors())throw  new ParamException(10001,"参数有误");
        Integer accountId = tokenUtil.getAccountId(token);
        UpdateGoodsParam param = BeanUtil.toBean(form, UpdateGoodsParam.class);
        Map map = JSON.parseObject(form.getAttribute(), Map.class);
        param.setAccountId(accountId);
        param.setGoodsAttribute(map);
        GoodsDto goods = goodsService.updateGoods(param);
        return Result.success("修改典当物品成功",goods);
    }

    /**
     * 根据id查找典当物品
     * @param goodsId 典当物品id
     * @return 典当物品对象
     * @throws Exception
     */
    @ApiOperation("根据id查找典当物品接口")
    @ApiImplicitParam("典当物品id")
    @ApiResponses({
            @ApiResponse(code = 200,message = "OK",response = GoodsDto.class)
    })
    @GetMapping("/findGoodsById")
    public Result findGoodsById(Integer goodsId) throws Exception{
        GoodsDto goods = goodsService.findGoodsById(goodsId);
        return Result.success("根据id查找典当物品成功",goods);
    }

    /**
     * 根据ids查找物品
     * @param form 典当物品分页查询对象
     * @return 典当物品对象分页对象
     * @throws Exception
     */
    @ApiOperation("根据ids查找物品")
    @ApiImplicitParam("典当物品分页查询对象")
    @ApiResponses({
            @ApiResponse(code = 200,message = "OK",response = GoodsDto.class)
    })
    @PostMapping("/findGoodsByIds")
    public Result findGoodsByIds(@RequestBody FindGoodsPageForm form) throws Exception{
        IPage<GoodsDto> goods = goodsService.findGoodsByIds(BeanUtil.toBean(form, FindGoodsPageParam.class));
        return Result.success("根据ids查找物品成功",goods);
    }

    /**
     * 条件分页查找物品
     * @param form 典当物品分页查询对象
     * @return 典当物品对象分页对象
     * @throws Exception
     */
    @ApiOperation("条件分页查找物品")
    @ApiImplicitParam("典当物品分页查询对象")
    @ApiResponses({
            @ApiResponse(code = 200,message = "OK",response = GoodsDto.class)
    })
    @PostMapping("/findGoodsPage")
    public Result findGoodsPage(@RequestBody FindGoodsPageForm form) throws Exception{
        IPage<GoodsDto> goods = goodsService.findGoodsPage(BeanUtil.toBean(form, FindGoodsPageParam.class));
        return Result.success("条件分页查找物品成功",goods);
    }



}

